﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Escuela.Forms
{
    public partial class CierreMensualForm : Form
    {
        private Instituto inst;
        protected int acep;

        public CierreMensualForm(Instituto inst)
        {
            this.inst = inst;
            InitializeComponent();
        }

        private void CierreMensual_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'escuelaDataSet.coop_cierre_mensual_GetAll' table. You can move, or remove it, as needed.
            this.coop_cierre_mensual_GetAllTableAdapter1.Fill(this.escuelaDataSet.coop_cierre_mensual_GetAll);
            // TODO: This line of code loads data into the 'dataSet8.coop_cierre_mensual_GetAll' table. You can move, or remove it, as needed.
           // this.coop_cierre_mensual_GetAllTableAdapter.Fill(this.dataSet8.coop_cierre_mensual_GetAll);

        }

        private void Nuevo_Click(object sender, EventArgs e)
        {
            try
            {
                acep = 1;
                dateTimePicker1.Enabled = false;
                disableBtn();
                limpiarBox();
                maskedNroCur.Enabled = false;

                DateTime fechaActual = Convert.ToDateTime(dateTimePicker1.Text.Trim());
                int mes = Convert.ToInt32(textBox1.Text.Trim());
                int año = Convert.ToInt32(textBox2.Text.Trim());
                CierreMensual cierreMensual = this.inst.registrarCierreMensual(mes, año);

              //  decimal monto = this.inst.mostrarCierreDiario(fechaActual);
                decimal monto = cierreMensual.MontoTotal;
                this.maskedNroCur.Text = Convert.ToString(monto);
                
                this.enableTxt();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al realizar el cierre" + ex.Message);
            }
        }

        //private void Confirmar_Click (object sender, EventArgs e)
        //{
        //    switch (acep)
        //    {
        //        #region Confirmar
        //        case 1:
        //            try
        //            {
        //                DateTime fechaActual = Convert.ToDateTime(dateTimePicker1.Text.ToString().Trim());
        //                int mes = Convert.ToInt32(textBox1.Text.Trim());
        //                int año = Convert.ToInt32(textBox2.Text.Trim());
        //                decimal diferencia = Convert.ToDecimal(this.maskedTextBox1.Text.Trim());
        //                string observacion = this.textDescrip.Text.Trim();
        //                CierreMensual cm = this.inst.registrarCierreMensual(mes, año);
        //                this.inst.completarDatosCierreMensual(cm, diferencia, observacion, fechaActual);
        //                this.inst.guardarCierreMensual(cm);
                        
        //                MessageBox.Show("Cierre finalizado con exito");
        //                limpiarBox();
        //                enableBtn();
                       

        //            }
        //            catch (Exception ex)
        //            {
        //                MessageBox.Show("Error al realizar el cierre" + ex.Message);
        //            }
        //            break;

        //        #endregion

        //        #region Buscar
        //        case 2:

        //            try
        //            {

        //                string where = "fecha= ' " + this.dateTimePicker1.Value;
        //                if (this.dateTimePicker1.Text != null && this.dateTimePicker1.Text.Trim() != "")
        //                {
        //                    where = where + "fecha= " + this.dateTimePicker1.Text.Trim().ToString();

        //                }


        //                if (where.Trim().Length > 3)
        //                    where = where.Substring(0, where.Length - 5);

        //                coopcierremensualGetAllBindingSource.Filter = where;

        //            }
        //            catch (Exception ex)
        //            {
        //                MessageBox.Show("Error al buscar cierre" + ex.Message);
        //            }
        //            break;

        //        #endregion
        //    }

        //}

        protected void enableTxt()
        {
            this.maskedTextBox1.Enabled = true;
            this.textDescrip.Enabled = true;
        }

        protected void disableTxt()
        {
            this.maskedNroCur.Enabled = false;
            this.maskedTextBox1.Enabled = false;
            this.textDescrip.Enabled = false;
        }
        protected void disableBtn()
        {
            this.Nuevo.Enabled = false;
            this.Confirmar.Enabled = true;
            this.Buscar.Enabled = false;
        }
        protected void limpiarBox()
        {
            this.maskedNroCur.Clear();
            this.maskedTextBox1.Clear();
            this.textDescrip.Clear();
        }
        protected void enableBtn()
        {
            this.Nuevo.Enabled = true;
            this.Confirmar.Enabled = false;
            this.Buscar.Enabled = true;
        }

        private void Buscar_Click(object sender, EventArgs e)
        {

        }

        private void Confirmar_Click_1(object sender, EventArgs e)
        {
            switch (acep)
            {
                #region Confirmar
                case 1:
                    try
                    {
                        DateTime fechaActual = Convert.ToDateTime(dateTimePicker1.Text.ToString().Trim());
                        int mes = Convert.ToInt32(textBox1.Text.Trim());
                        int año = Convert.ToInt32(textBox2.Text.Trim());
                        decimal diferencia = Convert.ToDecimal(this.maskedTextBox1.Text.Trim());
                        string observacion = this.textDescrip.Text.Trim();
                        CierreMensual cm = this.inst.registrarCierreMensual(mes, año);
                        this.inst.completarDatosCierreMensual(cm, diferencia, observacion, fechaActual);
                        this.inst.guardarCierreMensual(cm);

                        MessageBox.Show("Cierre finalizado con exito");
                        limpiarBox();
                        enableBtn();
                        this.coop_cierre_mensual_GetAllTableAdapter1.Fill(this.escuelaDataSet.coop_cierre_mensual_GetAll);

                      //  this.coop_cierre_mensual_GetAllTableAdapter.Fill(this.dataSet8.coop_cierre_mensual_GetAll);


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al realizar el cierre" + ex.Message);
                    }
                    break;

                #endregion

                #region Buscar
                case 2:

                    try
                    {

                        string where = "fecha= ' " + this.dateTimePicker1.Value;
                        if (this.dateTimePicker1.Text != null && this.dateTimePicker1.Text.Trim() != "")
                        {
                            where = where + "fecha= " + this.dateTimePicker1.Text.Trim().ToString();

                        }


                        if (where.Trim().Length > 3)
                            where = where.Substring(0, where.Length - 5);

                        coopcierremensualGetAllBindingSource.Filter = where;

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error al buscar cierre" + ex.Message);
                    }
                    break;

                #endregion
            }
        }

 

       
    }
}
